# # -*- coding:utf-8 -*-
# class Solution:
#     def Power(self, base, exponent):
#         # write code here
#         def helper(base_num, base, exponent):
#             if exponent == 0:
#                 return 1
#             if exponent == 1:
#                 return base_num
#             return helper(base_num * base, base, exponent - 1)
#         if exponent > 0:
#             return helper(base, base, exponent)
#         else:
#             return 1/helper(base, base, abs(exponent))
def fastPower(base, exponent):
    # 幂函数的快速乘法，将指数分解为2的整数次方的和
    if exponent == 0:
        return 1
    if exponent == 1:
        return base
    e = abs(exponent)
    tmp = base
    res = 1
    while (e > 0):
        print('e is %d' % e)
        if e & 1 == 1:
           res *= tmp
        e = e >> 1
        tmp *= tmp

        print('tmp is %d' % tmp)
        print('res is %d' % res)
    return res if exponent > 0 else 1/res
fastPower(2, 11)

class Solution:
    def Power(self, base, exponent):
        # write code here
        if exponent == 0:
            return 1
        if exponent == 1:
            return base
        res = 1
        def helper(res, base, exponent):
           if exponent <= 0:
               return res
           if exponent & 1 == 1:
               res *= base
           base *= base
           exponent = exponent >> 1
           return helper(res, base, exponent)
        res = helper(res, base, abs(exponent))
        return res if exponent > 0 else 1/res